<?php

/**
 * Frog CMS - Content Management Simplified. <http://www.madebyfrog.com>
 * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
 * Copyright (C) 2008 Martijn van der Kleijn <martijn.niji@gmail.com>
 * Copyright (C) 2008 Maslakov Alexander <jmas.ukraine@gmail.com>
 *
 * This file is part of Frog CMS.
 *
 * Frog CMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Frog CMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Frog CMS.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Frog CMS has made an exception to the GNU General Public License for plugins.
 * See exception.txt for details and the full text.
 */

/**
 * @package frog
 * @subpackage stylesheets
 *
 * @author Maslakov Alexandr <jmas.ukraine@gmail.com>
 * @version 0.1
 * @license http://www.gnu.org/licenses/gpl.html GPL License
 * @copyright Maslakov Alexander, 2010
 */


/*
	DEFINITIONS: Frog path
*/
define( 'FROG_ROOT', dirname(__FILE__) );

if( !defined('FROG_BACKEND') )
{
	define( 'FROG_BACKEND', false );
}




/*
	INCLUDING: Config
*/
// Including config file
if( file_exists( FROG_ROOT . '/config.php') )
{
	require_once( FROG_ROOT . '/config.php' );
}
else
{
	die('Please put "config.php" file at site root!');
}



/*
	INSTALLING
*/
if( !defined('DEBUG') )
{
	header('Location: '. (FROG_BACKEND ? '../' : '') .'install/');
	exit();
}



/*
	DEFINITION: Current page URI
*/
$__URI__ = '';

if( !empty($_SERVER['QUERY_STRING']) )
{
	$__URI__ = $_SERVER['QUERY_STRING'];
	
	if( strstr($__URI__, '&') !== false )
	{
		$__URI__ = substr( $__URI__, 0, strpos($__URI__, '&') );
	}
}

if( URL_SUFFIX != '' && strpos($__URI__, URL_SUFFIX) !== false )
{
	$__URI__ = substr( $__URI__, 0, strlen($__URI__) - strlen(URL_SUFFIX) );
}

define( 'CURRENT_URI', $__URI__ );



/*
	DEFINITION: BASE_URL
*/
if( FROG_BACKEND )
{	
	define( 'BASE_URL', URL_PUBLIC . ( substr(URL_PUBLIC, -1) == '/' ? '' : '/' ) . ADMIN_DIR . '/' . ( USE_MOD_REWRITE ? '' : '?/' ) );
}
else
{	
	define( 'BASE_URL', URL_PUBLIC . ( substr(URL_PUBLIC, -1) == '/' ? '' : '/' ) . ( USE_MOD_REWRITE ? '' : '?/' ) );
}



/*
	DEFINITIONS: Framework constants
*/
// Frog CMS root
define( 'FROG_VERSION', '1.0.0' );

// Frog CMS core root path
define( 'CORE_ROOT', FROG_ROOT . '/frog' );

// Frog CMS framework application path
define( 'APP_PATH', CORE_ROOT . '/app' );

// Framework defines
define( 'SESSION_LIFETIME', 3600 );
define( 'REMEMBER_LOGIN_LIFETIME', 1209600 ); // two weeks

define( 'DEFAULT_CONTROLLER', 'front' );
define( 'DEFAULT_ACTION', 'index' );

define( 'COOKIE_PATH', '/' );
define( 'COOKIE_DOMAIN', '' );
define( 'COOKIE_SECURE', false );



/*
	INCLUDING: Green framework
*/
require_once( CORE_ROOT . '/Framework.php' );



/*
	INITIALIZING: Default defines
*/
if( !defined('HELPER_PATH') )
{
	define( 'HELPER_PATH', CORE_ROOT . '/helpers' );
}

if( !defined('URL_SUFFIX') )
{
	define( 'URL_SUFFIX', '' );
}

if( !defined('PUBLIC_FILES') )
{
	define( 'PUBLIC_FILES', 'public' );
}

if( !defined('PLUGINS_URL') )
{
	define('PLUGINS_URL', URL_PUBLIC . 'frog/plugins/');
}

if( !defined('PLUGINS_DIR') )
{
	define('PLUGINS_DIR', FROG_ROOT . '/plugins/');
}



/*
	INITIALIZING: Connecting to Database
*/
if( USE_PDO && class_exists('PDO', false) )
{
	// adding function date_format to sqlite 3 'mysql date_format function'
	if( !function_exists('mysql_date_format_function') )
	{
		function mysql_function_date_format($date, $format)
		{
			return strftime($format, strtotime($date));
		}
	}

    try
	{
        $__CONN__ = new PDO( DB_DSN, DB_USER, DB_PASS );
	} 
	catch( PDOException $error )
	{
        die( 'DB Connection failed: ' . $error->getMessage() );
	}
    
	switch( $__CONN__->getAttribute(PDO::ATTR_DRIVER_NAME) )
	{
		// MySQL settings
		case 'mysql':
			$__CONN__->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
			break;
		
		// SQLite3 settings
		case 'sqlite':
			$__CONN__->sqliteCreateFunction('date_format', 'mysql_function_date_format', 2);
			break;
	}		
}
else
{
    require_once( CORE_ROOT . '/libraries/DoLite.php' );
    $__CONN__ = new DoLite(DB_DSN, DB_USER, DB_PASS);
}

// Assign DB Connection to Record
Record::connection( $__CONN__ );
Record::getConnection()->exec('set names "utf8"');

// Backward compatibility
$__FROG_CONN__ = $__CONN__; // Frog CMS 0.95
$__WOLF_CONN__ = $__CONN__; // Wolf CMS



/*
	INITIALIZING: Class init
*/
Setting::init();

// Get user locale
AuthUser::load();
$__LOCALE__ = (AuthUser::isLoggedIn() ? AuthUser::getRecord()->language : Setting::get('language'));

// Set system localization
use_helper('I18n');
I18n::setLocale($__LOCALE__);

Plugin::init();
Flash::init();



/*
	RUN Dispatcher
*/
if( FROG_BACKEND )
{
	$default_tab = Setting::get('default_tab');
	$default_tab = empty($default_tab) ? 'page/index' : $default_tab;
	
	Dispatcher::addRoute(array(
		'/' => $default_tab,
		'/:any' => '$1'
	));
}
else
{
	Dispatcher::addRoute(array(
		'/:any' => 'front/index/$1'
	));
}

Dispatcher::dispatch();